package com.example.apple.hxbtc.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.apple.hxbtc.R;
import com.example.apple.hxbtc.activity.UserLoginActivity;

/**
 * Created by hpz on 17/2/21.
 */

public class TradeFragment extends Fragment implements RadioGroup.OnCheckedChangeListener,ViewPager.OnPageChangeListener{

    private ViewPager mViewPager;
    private RadioGroup mRadioGroup;
    private StudyListPagerAdapter adapter;
    private int[] dataArray;

    private TextView textView_login;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    private void initView() {

        mRadioGroup = (RadioGroup) getActivity().findViewById(R.id.groups_list);
        mRadioGroup.setOnCheckedChangeListener(this);
        mViewPager = (ViewPager) getActivity().findViewById(R.id.vp_list);
        adapter = new StudyListPagerAdapter(getActivity().getSupportFragmentManager(),null);
        mViewPager.setAdapter(adapter);
        mViewPager.setOnPageChangeListener(this);

        mRadioGroup.check(mRadioGroup.getChildAt(0).getId());
        textView_login = (TextView) getActivity().findViewById(R.id.login_button);
        textView_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_login = new Intent();
                intent_login.setClass(getActivity(), UserLoginActivity.class);
                startActivity(intent_login);
            }
        });
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        int index = radioGroup.indexOfChild(radioGroup.findViewById(i));
        mViewPager.setCurrentItem(index);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

        View childView = mRadioGroup.getChildAt(position);
        if (childView instanceof RadioButton){
            RadioButton radioButton = (RadioButton) childView;
            mRadioGroup.check(radioButton.getId());
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_trade, container, false);
        return view;
    }
}
